﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using Xiaov.SmartWeb.Model;

namespace Xiaov.SmartWeb.Application.TZGC
{
    public partial class GCView : BasePage
    {
        private string _moduleid;
        public string ModuleID
        {
            get
            {
                if (Request.QueryString["mid"] != null)
                {
                    _moduleid = Request.QueryString["mid"].ToString();
                }
                else
                {
                    _moduleid = "0000";
                }
                return _moduleid;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            PM_Filter();
            if (!IsPostBack)
            {
                //绑定年度
                BindND();
                //绑定工程列表
                //BindGridGC();
                //绑定项目列表
                BindGridXM();
            }
        }

        //绑定年度下拉框
        protected void BindND()
        {
            int year_cur = DateTime.Now.Year;
            int month_cur = DateTime.Now.Month;

            for (int i = year_cur - 10; i <= year_cur + 10; i++)
            {
                DropDownList1.Items.Add(i.ToString());
            }
            DropDownList1.SelectedValue = year_cur.ToString();
            //DropDownList2.SelectedValue = month_cur.ToString();
        }

        //绑定GRID
        protected void BindGridGC()
        {
            string xmbm;
            //xmbm = currrow.Cells[1].Text.ToString();
            xmbm = Label2.Text.ToString().Trim();

            StringBuilder sql_query = new StringBuilder();
            sql_query.Append("select rownum,a.gcbm,a.gcmc,a.gcnd,a.gcyd,a.ssxmbm,b.xmmc,a.sx,a.sstzxmmc,a.rowid from yt_gcb a,yt_xmb b where a.ssxmbm = b.xmbm(+)  ");
            sql_query.Append(" and ssxmbm = '" + xmbm + "' ");
            int nd, yd;

            if (CheckBox1.Checked == true)
            {
                nd = Convert.ToInt32(DropDownList1.SelectedItem.ToString());
                sql_query.Append(" and a.gcnd = " + nd + " ");
            }

            if (CheckBox2.Checked == true)
            {
                yd = Convert.ToInt32(DropDownList2.SelectedItem.ToString());
                sql_query.Append(" and a.gcyd = " + yd + "  ");
            }

            sql_query.Append("order by a.sx asc");
            GridViewGC.DataSource = Maticsoft.DBUtility.DbHelperOra.Query(sql_query.ToString());
            GridViewGC.DataBind();

        }

        //将选中项目的所属工程的复选框选中
        protected void XM_Select_GC()
        {
            foreach (GridViewRow row in GridViewGC.Rows)
            {
                CheckBox cbox = (CheckBox)row.FindControl("cbxId");
                cbox.Checked = false;
            }


            if (GridViewXM.SelectedIndex >= 0)
            {
                GridViewRow currrow = GridViewXM.SelectedRow;
                string xmbm;
                //xmbm = currrow.Cells[1].Text.ToString();
                xmbm = Label2.Text.ToString().Trim();
                string sql_query;
                sql_query = "select gcbm from yt_gcb where ssxmbm = '" + xmbm + "'";
                OracleDataReader mydr;
                mydr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(sql_query);

                StringBuilder gcbmList = new StringBuilder();
                while (mydr.Read())
                {
                    gcbmList.Append(mydr["gcbm"].ToString());
                    gcbmList.Append(",");
                }

                foreach (GridViewRow row in GridViewGC.Rows)
                {

                    if (gcbmList.ToString().Contains(row.Cells[2].Text.ToString()))
                    {
                        CheckBox cbox = (CheckBox)row.FindControl("cbxId");
                        cbox.Checked = true;
                    }
                }
            }
        }


        //绑定GRID
        protected void BindGridXM()
        {
            StringBuilder sql_query = new StringBuilder();
            sql_query.Append("select rownum,a.xmbm,a.xmmc,a.xmnd,a.xmyd,a.sslbbm,b.lbmc,a.sx,a.rowid from yt_xmb a,yt_xmlbb b where a.sslbbm = b.lbbm ");

            int nd, yd;

            if (CheckBox1.Checked == true)
            {
                nd = Convert.ToInt32(DropDownList1.SelectedItem.ToString());
                sql_query.Append(" and a.xmnd = " + nd + " ");
            }

            if (CheckBox2.Checked == true)
            {
                yd = Convert.ToInt32(DropDownList2.SelectedItem.ToString());
                sql_query.Append(" and a.xmyd = " + yd + "  ");
            }

            sql_query.Append("order by a.sx asc");
            GridViewXM.DataSource = Maticsoft.DBUtility.DbHelperOra.Query(sql_query.ToString());
            GridViewXM.DataBind();

        }

        //项目翻页
        protected void GridViewXM_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewXM.PageIndex = e.NewPageIndex;//分页BindData();
            BindGridXM();
        }

        //选中项目
        protected void GridViewXM_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    e.Row.Attributes["style"] = "cursor:hand";
                    #region   //点击行触发SelectedIndexChanged事件
                    PostBackOptions myPostBackOptions = new PostBackOptions(this);
                    myPostBackOptions.AutoPostBack = false;
                    myPostBackOptions.PerformValidation = false;
                    myPostBackOptions.RequiresJavaScriptProtocol = true; //加入javascript:头
                    String evt = Page.ClientScript.GetPostBackClientHyperlink(sender as GridView, "Select$" + e.Row.RowIndex.ToString());
                    e.Row.Attributes.Add("onclick", evt);
                    #endregion
                    break;
            }


        }

        //工程翻页
        protected void GridViewGC_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewGC.PageIndex = e.NewPageIndex;//分页BindData();
            //BindGridGC();
            //将选中项目的所属工程的复选框选中
            XM_Select_GC();
        }

        //变色
        protected void GridViewGC_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标移动到某行上，该行变色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#dddddd'");
                //鼠标移开后，恢复
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                e.Row.Cells[1].Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            //BindGridGC();
            BindGridXM();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewGC.DataSource = null;
            GridViewGC.DataBind();
            BindGridXM();
        }

        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            //BindGridGC();
            BindGridXM();
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //BindGridGC();
            BindGridXM();
        }


        protected void GridViewXM_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = GridViewXM.SelectedRow;
            string xmbm;
            xmbm = row.Cells[1].Text.ToString();
            Label2.Text = xmbm.ToString().Trim();
            Label1.Visible = true;
            Label1.Text = "选中的项目是 " + row.Cells[2].Text + "";
            BindGridGC();
            XM_Select_GC();
        }

        //隐藏项目表中的列
        protected void GridViewXM_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Visible = false;
                e.Row.Cells[1].Visible = false;
                e.Row.Cells[5].Visible = false;
                e.Row.Cells[6].Visible = false;
                e.Row.Cells[7].Visible = false;
                e.Row.Cells[8].Visible = false;
            }

        }

        //隐藏工程表中的列
        protected void GridViewGC_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[2].Visible = false;
                e.Row.Cells[6].Visible = false;
                e.Row.Cells[7].Visible = false;
                e.Row.Cells[8].Visible = false;
                //e.Row.Cells[9].Visible = false;
                e.Row.Cells[10].Visible = false;
            }
        }


        //选择工程后保存
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Label2.Text != "无")
            {
                foreach (GridViewRow row in GridViewGC.Rows)
                {
                    CheckBox cbox = (CheckBox)row.FindControl("cbxId");
                    StringBuilder sql_update = new StringBuilder();
                    sql_update.Append("update yt_gcb set ");
                    if (cbox.Checked == false)
                    {
                        if (row.Cells[6].Text.ToString().Trim() == Label2.Text.ToString().Trim())
                        {
                            sql_update.Append(" ssxmbm = null ");
                            sql_update.Append(" where rowid = '" + row.Cells[10].Text.ToString().Trim() + "'");
                            Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sql_update.ToString());
                        }
                        else
                        {
                            sql_update.Append(" ssxmbm = ssxmbm ");
                            sql_update.Append(" where rowid = '" + row.Cells[10].Text.ToString().Trim() + "'");
                            Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sql_update.ToString());
                        }
                    }

                    //if (cbox.Checked == true)
                    //{
                    //    sql_update.Append(" ssxmbm = '" + Label2.Text.ToString().Trim() + "'");
                    //}

                    
                }
            }

            //保存之后必须要重新绑定工程GRID，因为工程的编码及所属项目都变了，再检索就检索不到了
            BindGridGC();
            XM_Select_GC();

        }
    }
}